using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class BlogDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/BlogData
        [Queryable]
        public IEnumerable<BlogData> GetBlogData()
        {
            return db.BlogDatas.AsEnumerable();
        }

        // GET api/BlogData/5
        [Queryable]
        public BlogData GetBlogData(String id)
        {
            BlogData blogdata = db.BlogDatas.Find(id);
            if (blogdata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return blogdata;
        }

        // PUT api/BlogData/5
        public HttpResponseMessage PutBlogData(String id, BlogData blogdata)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != blogdata.blogid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(blogdata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/BlogData
        public HttpResponseMessage PostBlogData(BlogData blogdata)
        {
            if (ModelState.IsValid)
            {
                db.BlogDatas.Add(blogdata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, blogdata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = blogdata.blogid }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/BlogData/5
        public HttpResponseMessage DeleteBlogData(String id)
        {
            BlogData blogdata = db.BlogDatas.Find(id);
            if (blogdata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.BlogDatas.Remove(blogdata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, blogdata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}